package com.cmxh.shipper.controller;

import com.cmxh.shipper.pojo.AddressBook;
import com.cmxh.shipper.pojo.Station;
import com.cmxh.shipper.service.AddressBookService;
import com.cmxh.shipper.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.cmxh.shipper.util.Code.GET_ERR;
import static com.cmxh.shipper.util.Code.GET_OK;

/*
 *
 *地址蒲Controller
 * */
@RestController
@RequestMapping("/addressbook")
public class AddressBookcontroller {
    @Autowired
    private AddressBookService addressBookService;

    @RequestMapping("/cha/{uid}")
    public Result selectAddressBook(@PathVariable Integer uid) {

        List<AddressBook> addressBooks = addressBookService.selectAddressBook(uid);
        System.out.println(addressBooks);
        if (addressBooks != null){
            return new Result(GET_OK,addressBooks,"查询成功");
        }else {
            return new Result(GET_ERR,"查询失败");
        }
    }
    @RequestMapping("/add")
    public Result insertAddressBook(@RequestBody AddressBook addressBook) {
        AddressBook existing = addressBookService.selectAddressBook01(addressBook.getZanjduanid() ,addressBook.getUid());

        if (existing != null) {
            // 3. 存在，返回提示信息
            return new Result(GET_ERR, "地址已存在");
        }else {
            // 4. 不存在，执行新增操作
            int i = addressBookService.insertAddressBook(addressBook);
            if (i > 0) {
                return new Result(GET_OK, "添加成功");
            } else {
                return new Result(GET_ERR, "添加失败");
            }
        }
    }
    @RequestMapping("/delete/{id}")
    public Result deleteAddressBook(@PathVariable Integer id) {

        int i = addressBookService.deleteAddressBook(id);
        if (i > 0){
            return new Result(GET_OK,"删除成功");
        }else {
            return new Result(GET_ERR,"删除失败");
        }
    }



}